<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\Goodshops as GoodshopsModel;

use app\common\model\Shucai as ShucaiModel;
use app\common\model\Goods as GoodsModel;
use app\common\controller\Upload;

use app\common\model\Order;

class Goodshops extends Fornt{

  public function index(){

    $slist = GoodshopsModel::where(['type'=>0, 'status'=>1])->select();
    $wlist = GoodshopsModel::where(['type'=>1, 'status'=>1])->select();

    $this->assign('slist',$slist);
    $this->assign('wlist',$wlist);
    return $this->fetch();
  }

  //蔬菜显示
  public function shucai(){
      parent::wechatJs();
      
      ///判断是不是管理人员
      $openid = model('WxUser')->where('id', USER_ID)->value('openid');
      $info = parent::getWxUserInfo($openid);
      //0普通用户    3 蔬菜权限用户   6  添加商品用户    9 最高用户
      $dlproxy = $info['dlproxy'];
      
      
      
     
//       if(input('startid')){
//           $mapss['a.id'] = array('egt',input('startid'));;
//       }
   if(input('scname')){
            $map['shucainame'] = array('like', '%'.input('scname').'%');
        }
       
//       $mapss['a.buystatus'] = array('eq','3');
    
//       if(input('fhren')==1){
//           $mapss['a.fhuser_id'] = array('in','4663,4796');;
//       }
//       if(input('fhren')==2){
//           $mapss['a.fhuser_id'] = 4704;
//       }
      $map['status'] = 1;
      $dllist = ShucaiModel::where($map)->order('bianhao asc')->select();
      
 
      $this->assign('dllist', $dllist);
      $this->assign('dlproxy',$dlproxy);

 
      return $this->fetch();
  }
  
  //水果显示
  public function shuiguo(){
      parent::wechatJs();
  
      ///判断是不是管理人员
      $openid = model('WxUser')->where('id', USER_ID)->value('openid');
      $info = parent::getWxUserInfo($openid);
      //0普通用户    3 蔬菜权限用户   6  添加商品用户    9 最高用户
      $dlproxy = $info['dlproxy'];

      
      
      
      //select a.id,a.cat_id,a.good_name,a.`status`,a.update_time,b.id,b.rule_name,b.stock,b.rule_price2,b.rule_price3,b.rule_price4 from xsd_goods  a
      //LEFT JOIN xsd_goodsrule b on b.goods_id=a.id
      //where a.cat_id=23 and a.update_time>1546324043 ORDER BY a.`status` desc,a.update_time desc
      $mapss['a.cat_id'] = 23;
      $dllist = GoodsModel::whereTime('a.update_time','>', '1546324043')->where($mapss)->alias('a')
      ->join('xsd_goodsrule b','b.goods_id=a.id','LEFT') 
      ->field('a.id  as goodid,a.cat_id,a.good_name,a.`status`,a.update_time,b.id,b.rule_name,b.stock,b.rule_price,b.rule_price2,b.rule_price3,b.rule_price4')
      ->order('a.`status` desc,a.update_time desc')
      ->select();
  
  
      $this->assign('dllist', $dllist);
      $this->assign('dlproxy',$dlproxy);
  
  
      return $this->fetch();
  }
  
  //前台水果上下架修改
  public function editStatus($id , $status){
//       echo  $id;
//       exit();
      if (empty($id) && empty($status)) {
          return $this->error("非法操作！");
      }
      $result = GoodsModel::where('id',$id)->setField('status', $status);
      if($result){
          return $this->success("修改成功！");
      }else{
          return $this->error("修改失败！");
      }
  }
  
  
  //修改水果价格和库存

  public function editPrice($id, $price, $price2,$price3,$stock){
	    $fhuser_id = USER_ID;
	 	    
	    if (empty($id)) {
	        return $this->error("非法操作！");
	    }
	    	    
	    if(IS_POST){
	    
	    
	        $return = db('Goodsrule')->where('id', $id)->update(['rule_price' => $price,'rule_price2'=>$price2, 'rule_price3'=>$price3,'stock'=>$stock]);

	        //修改更新时间查找数据
	        $dllistone = db('Goodsrule')->where('id', $id)->select();
	        $goodsid = model('Goodsrule')->where('id', $id)->value('goods_id');
	        
	       
	        //更新数据
	        db('Goods')->where('id', $goodsid)->update(['update_time'=>time()]);
	       	        
	        if($return){
	       
	            $this->success('修改成功！');
	        }else{
	            $this->error('修改失败失败！');
	        }
	    
	         return $this->error("非法操作！");
	    }
	}
  
  //蔬菜添加
  public function shucaiinsert(){
  
     //设置参数
     //插入数据
     //跳转页面

  
      if(IS_POST){
           
       
         //input('post.total')
         //$result->yunfei = input('yunfei');
          // 	    $result['total'] = input('post.total');
          $result['bianhao']= input('bianhao');
          $result['shucainame']= input('shucainame');
          $result['danwei'] = input('guige');
          $result['price1'] = input('priceone');
          $result['price2'] = input('pricetwo');
         
          $result = ShucaiModel::create($result);
           
          if(false !== $result){
      
              $this->success('添加商品成功！');
          }else{
              $this->error('添加商品失败！');
          }
           
          return $this->error("非法操作！");
      }
  }
  
  
  //修改蔬菜价格
  
  public function editShucai($id, $price1, $price2){
      
//       echo '--------'.$id.'--------'.$price1.'--------'.$price2.'--------';
//       exit();
      $fhuser_id = USER_ID;
       
      if (empty($id)) {
          return $this->error("非法操作！");
      }
  
      if(IS_POST){
           
           
          $return = ShucaiModel::where('id', $id)->update(['price1' => $price1,'price2'=>$price2]);
  
           
          if($return){
  
              $this->success('修改成功！');
          }else{
              $this->error('修改失败失败！');
          }
           
          return $this->error("非法操作！");
      }
  }
  
  
  //图片显示
  public function testimage(){

      //活动订单id
      $ddbhid=$_REQUEST['ddbhid'];
      //插入图片
      $testupload = new Upload();
    
      $res=$testupload->upload();
      
      
      
 
        //活动图片id
          $ddimage=$res['info']['id'];
          
         //把活动插入到订单表当中
          $result = db('Order')->where('id',$ddbhid)->setField('ddimage', $ddimage);
         //完成订单
          if ($result !== false) {
          
              return $this->success('操作成功！');
          
          } else {
          
              return $this->error('操作失败！');
          
          }
      
      
     
     
//       $testupload = new Upload();
//       $testupload->testecho();
//    exit();
//       $res=1;
//       return $this->fetch();
//       if($res==1){
           
//           $this->success('修改成功！');
//       }else{
//           $this->error('修改失败失败！');
//       }
//       return $this->error("非法操作！");
      
  }
  
  
  //图片显示
  public function testfile(){
  
            $testupload = new Upload();
           // $testupload->upload();           
             $res=$testupload->upload();
             //$obj=json_decode($res);             
//             echo"</br>"."aaaabbbbccc";
//             echo"</br>"."1111====".$res;
//             echo"</br>"."1111====".$res['status'];        
//             echo"</br>"."1111====".$res['info']['id'];
//          if($res['status']==1){
         
//              $this->success('修改成功！');
//          }else{
//              $this->error('修改失败失败！');
//          }
//          return $this->error("非法操作！");
         
         
     // return $this->fetch();
  }
  
  public function detail($id){
    $info = GoodshopsModel::get($id);
    $this->assign('info', $info);
    return $this->fetch();
  }

}
